package com.lib.net.util;

import android.content.Context;


import com.lib.net.ResponseError;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Joey on 2017/12/28.
 * 获取测试Json文本
 */
public class TestJsonManager {

    public static String getString(Context context, String fileName) {
        return readAssets(context, fileName, ".json");
    }

    public static String readAssets(Context context, String fileName, String suffix) {
        try {
            //Return an AssetManager instance for your application's package
            InputStream is = context.getAssets().open(fileName + "." + suffix);
            int size = is.available();
            // Read the entire asset into a local byte buffer.
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            // Convert the buffer into a string.
            String text = new String(buffer, "utf-8");
            // Finally stick the string into the text view.
            return text;
        } catch (IOException e) {
            // Should never happen!
//            throw new RuntimeException(e);
            e.printStackTrace();
        }

        return new ResponseError(ResponseError.ERROR_UN_KNOWN, "读取错误，请检查文件名").toString();
    }
}
